package com.blog.wu.myblog.exception;

import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
import org.apache.ibatis.javassist.NotFoundException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 全局异常处理类
 * @author jty
 * @version 2015-03-08
 */
@Component
public class ExceptionResolver extends SimpleMappingExceptionResolver {
    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        // Expose ModelAndView for chosen error view.
        String viewName = determineViewName(ex, request);

        if (ex.getClass().equals(UnauthorizedException.class)) {
            //403
            return getModelAndView("/error/403", ex, request);
        }

        if (ex.getClass().equals(NotFoundException.class)) {
            //404
            return getModelAndView("/error/404", ex, request);
        }

        if (ex.getClass().equals(InternalException.class)) {
            //500
            return getModelAndView("/error/500", ex, request);
        }

        return null;
    }


}
